package cn.openea.perpartner.cms.web.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.openea.perpartner.cms.factory.UserContext;
import cn.openea.perpartner.cms.web.dto.CommodityDto;
import cn.openea.perpartner.cms.web.entity.CmsCommodity;
import cn.openea.perpartner.cms.web.service.ICmsCommodityService;
import cn.openea.perpartner.common.core.web.controller.BaseController;
import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import cn.openea.perpartner.common.core.web.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;

/**
 * (CmsCommodity)表控制层
 *
 * @author makejava
 * @since 2022-08-23 12:10:40
 */
@Api(tags = "商品子表")
@Controller
@RequestMapping("commodity")
public class CmsCommodityController extends BaseController {
    /**
     * 服务对象
     */
    @Autowired
    private ICmsCommodityService commodityService;

    /**
     * 分页查询所有数据
     * @param customerId 品牌商家id
     * @return 所有数据
     */
    @ApiOperation("商品信息")
    @GetMapping("/detail/list")
    @ResponseBody
    public TableDataInfo selectList(String customerId) {
        List<CmsCommodity> list = this.commodityService.selectList(customerId);
        return getDataTable(list);
    }

    /**
     * 新增数据
     *
     * @param commodity 实体对象
     * @return 新增结果
     */
    @ApiOperation("添加商品")
    @PostMapping("/detail/add")
    @ResponseBody
    public AjaxResult save(@RequestBody CmsCommodity commodity) {
        commodity.setCreateUser(UserContext.getUserId());
        return success(this.commodityService.save(commodity));
    }
    /**
     * 通过主键查询单条数据
     * @param commodityId 主键
     * @return 单条数据
     */
    @ApiOperation("商品明细")
    @GetMapping("{commodityId}")
    @ResponseBody
    public AjaxResult selectOne(@PathVariable Serializable commodityId) {
        return success(this.commodityService.getById(commodityId));
    }
    /**
     * 修改数据
     *
     * @param commodity 实体对象
     * @return 修改结果
     */
    @ApiOperation("修改商品")
    @PutMapping("/detail/edit")
    @ResponseBody
    public AjaxResult update(@RequestBody CmsCommodity commodity) {
        commodity.setUpdateUser(UserContext.getUserId());
        return success(this.commodityService.updateById(commodity));
    }

    /**
     * 删除数据
     *
     * @return 删除结果
     */
    @ApiOperation("删除商品")
    @DeleteMapping("/detail/delete")
    @ResponseBody
    public AjaxResult delete(@RequestBody List<Long> idList) {
        return success(this.commodityService.removeByIds(idList));
    }

    /**
     * 商品定价
     * @return 删除结果
     */
    @ApiOperation("更新商品")
    @PutMapping("/edit")
    @ResponseBody
    public AjaxResult update(@RequestBody List<CommodityDto> commodityDtoList) {
        return commodityService.updateBatch(commodityDtoList)?success():error();
    }
    /**
     * 分页查询所有数据
     * @return 所有数据
     */
    @ApiOperation("商品列表")
    @GetMapping("list")
    @ResponseBody
    public TableDataInfo selectAll(String userId,String searchStr,String commodityStatus) {
        List<CmsCommodity> list = commodityService.selectAll(userId,searchStr,commodityStatus);
        return getDataTable(list);
    }

    @ApiOperation("渠道选品")
    @PostMapping("/preview")
    @ResponseBody
    public AjaxResult preview(@RequestBody List<Long> idList) throws IOException {
        return success(commodityService.preview(idList));
    }

}

